package com.handler;

import com.message.ExitGroupChatReqMessage;
import com.message.resp.ExitGroupResp;
import com.session.ChatServerContextFactory;
import com.session.GroupChatServerFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import javax.sound.midi.Track;
import java.util.Collections;

/**
 * 退出群聊的 handler
 */
@ChannelHandler.Sharable
public class ExitGroupHandler extends SimpleChannelInboundHandler<ExitGroupChatReqMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ExitGroupChatReqMessage msg) throws Exception {
//          获取当前登录用户的名字
        String currentUserName = ChatServerContextFactory.getInstance().getUserNameByChannel(ctx.channel());
//          退出聊天组
        GroupChatServerFactory.getInstance().exitChatGroup(msg.getGroupName(), Collections.singletonList(currentUserName));

        ctx.channel().writeAndFlush(new ExitGroupResp(true));
    }
}
